package com.exam.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.exam.domain.Answer;
import com.exam.domain.DTO.ExamingDTO;
import com.exam.handler.StpHandler;
import com.exam.service.ExamService;
import com.exam.service.ExamStudentService;
import com.exam.service.ExamTeacherService;
import com.exam.utils.ResultBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "考试过程管理")
@RestController
@RequestMapping(value = "/examing")
public class ExamingController {

    @Autowired
    private ExamService examService;

    @Autowired
    private ExamStudentService examStudentService;

    /**
     * 教师手动开始考试
     * @param examId 考试id
     * @return 考试信息
     */
    @GetMapping("/startExam/{examId}")
    @ApiOperation(value = "教师手动开始考试", notes = "教师手动开始考试")
    public ResultBody startExam(@PathVariable int examId) {
        return ResultBody.success(examService.startExam(examId));
    }

    /**
     * 教师手动结束考试/前端计时器结束自动调用
     * @param examId 考试id
     * @return 考试信息
     */
    @GetMapping("/finishExam/{examId}")
    @ApiOperation(value = "结束考试", notes = "结束考试")
    @SaCheckPermission(value = {"teacher", "student"}, mode = SaMode.OR)
    public ResultBody endExam(@PathVariable int examId) {
        return ResultBody.success(examService.finishExam(examId));
    }


    /**
     * 学生提交答题卡
     * @param examId 考试id
     * @return 答题卡信息
     */
    @GetMapping("/finishAnswerSheet")
    @ApiOperation(value = "提交答题卡", notes = "提交答题卡")
    @SaCheckPermission("student")
    public ResultBody finishAnswerSheet(@RequestParam int examId) {
        return ResultBody.success(examStudentService.finishAnswerSheet(examId, StpHandler.getId()));
    }

    /**
     * 学生获取考试信息
     * @param examId 考试id
     * @return 考试信息
     */
    @GetMapping("/getExamInfo")
    @ApiOperation(value = "获取考试信息", notes = "获取考试信息")
    @SaCheckPermission("student")
    public ResultBody getExamInfo(@RequestParam int examId) {
        return ResultBody.success(examStudentService.getExamInfo(examId, StpHandler.getId()));
    }


    /**
     * 学生上传答案（不提交答题卡）
     * @param examId 考试id
     * @param examingDTOList 相关信息
     * @return 修改后的信息
     */
    @PostMapping("/postExamInfo")
    @ApiOperation(value = "上传答案（不提交答题卡）", notes = "上传答案（不提交答题卡）")
    @SaCheckPermission("student")
    public ResultBody postExamInfo(@RequestParam int examId, @RequestBody List<ExamingDTO> examingDTOList) {
        return ResultBody.success(examStudentService.postExamInfo(examId, StpHandler.getId(), examingDTOList));
    }

    /**
     * 认证是否可以参加考试
     * @param examId 考试id
     * @return 答题卡信息
     */
    @GetMapping("/getExamAccess")
    @ApiOperation(value = "认证是否可以参加考试", notes = "认证是否可以参加考试")
    @SaCheckPermission("student")
    public ResultBody getExamAccess(@RequestParam int examId) {
        return ResultBody.success(examStudentService.getExamAccess(examId, StpHandler.getId()));
    }

}
